
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue-2.4.0.js" ></script>
    <!-- <script src="vue1026.js" ></script> -->
</head>

<style>

    #tab{
        width:800px;
        border-collapse: collapse;
        margin: 20px auto;
    }
    #tab th{
        background-color: #0094ff;
        color: white;
        border: solid  black 2px;
        text-align: center;
    }

    #tab td{
        border: solid  black 2px;
        text-align: center;
    }

    .inp{
        width:800px;
        border-collapse: collapse;
        margin: 20px auto;
    }

</style>
<body>
       
        <div id="app">
            <div class="inp">
            <input type="text" v-model="id" />
            <input type="text" v-model="pname" />
            <input type="text" v-model="age" @keydown.f2="addData" />
            <input type="text" v-model="sname" placeholder="请输入" />  <!--  1.0版本实现搜索-->
            <button class="in" v-on:click="addData">添加</button>

        </div>
        <table id="tab">
            <tr>
                <th>Id</th>
                <th>名字</th>
                <th>年龄</th>
                <th>时间</th>
                <th>操作</th>
            </tr>
            <tr v-if="list.length == 0">
                <td colspan="6">此列表无数据</td>
            </tr>
            <!-- 1.0版本适合 -->
            <!-- <tr v-for="item in list | filterBy sname in 'name'"> -->
            <tr v-for="item in list">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td>{{item.ctime}}</td>
                <td><a href="javascript:void(0)" @click="delData(item.id)">删除</a></td>
            </tr>

        </table>
    </div>
</body>

<script>
            // 2.0版本
    Vue.config.keyCodes.f2 = 113;
    console.log(Vue.config.keyCodes);

    var vm = new Vue({
        el:'#app',
        data:{
            list:[
            {id:1,name:'小明',age:'28',ctime:new Date()}
           
        ],
        id:0,
        pname:'',
        age:0,
        sname:'',

        },
        methods:{
            addData:function(){
                var p = {id:this.id,name:this.pname,age:this.age,ctime:new Date()}
                this.list.push(p);
                this.id=0;
                this.pname = '';
                this.age = 0;

            },
            delData:function(id){
                if(!confirm('是否要删除?')){
                    alert('1')
                    return;
                }
                var index = this.list.findIndex(function(item){ return item.id == id});
                this.list.splice(index,1);
            }

        }
    });

</script>
</html>